/*
 * @Author: silence silence9685@163.com
 * @Date: 2023-07-19 14:18:02
 * @LastEditors: silence silence9685@163.com
 * @LastEditTime: 2023-07-21 11:44:10
 * @FilePath: \ems_neutral_app\lib\pages\Mine\AccountManager\EditPhonePage.dart
 * @Description: 绑定/修改手机号
 */
import 'package:ems_neutral_app/base/SilScaffold.dart';
import 'package:ems_neutral_app/common/widgets/cubitWidget/BottomSheetView.dart';
import 'package:ems_neutral_app/config/AppInfo.dart';
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';
import 'package:sil_utils_package/tools/SilTools.dart';

import '../../../common/Method/CommonMethod.dart';
import '../../../common/widgets/cubitWidget/InputView.dart';
import '../../../common/widgets/cubitWidget/SubmitBtn.dart';
import '../../../utils/color/ColorUtils.dart';
import '../../../utils/router/ARouter.dart';
import 'Controller/AccountManagerController.dart';

class EditPhonePage extends StatefulWidget {
  const EditPhonePage({super.key});

  @override
  State<EditPhonePage> createState() => _EditPhonePageState();
}

class _EditPhonePageState extends State<EditPhonePage> {
  AccountManagerController _accountManagerController =
      Get.put(AccountManagerController());

  String? _phone;

  /// 区号
  late String _areaCode;
  late List<String> _telCodeList;
  String? _newPhone;

  /// 验证码
  String? _code;

  /// 是否进行绑定手机号，默认绑定
  bool _isBinding = true;

  @override
  void initState() {
    super.initState();

    _phone = _accountManagerController.userPhone.value;
    _areaCode = _accountManagerController.areaCode;
    _telCodeList = kTelCodeList;

    /// 判断是否进行绑定手机号：有手机号则是已经绑定过，现在进行修改手机号
    if (!SilTools.stringIsEmpty(_phone) && _phone!.length > 0) {
      _isBinding = false;
    }
  }

  @override
  Widget build(BuildContext context) {
    return SilScaffold(
      title: _isBinding
          ? LangOf(context)?.user?.bind_phone
          : LangOf(context)?.user?.edit_phone,
      body: mainWidget(),
    );
  }

  mainWidget() {
    return Container(
      margin: EdgeInsets.only(top: 40.w),
      child: Column(
        children: [
          phoneNumWidget(),
          captchaWidget(),
          if (!_isBinding) newPhoneWidget(),
          submitBtnWidget(),
        ],
      ),
    );
  }

  phoneNumWidget() {
    return _isBinding
        ? InputView(
            title: LangOf(context)?.editPhone?.phone,
            hintText: LangOf(context)?.editPhone?.phonephl,
            inputViewType: InputViewType.InputViewTypePhone,
            leftView: SilButton(
              padding: EdgeInsets.only(right: 5.w),
              imgPath: 'imgs/common/common_sanjx.png',
              imgHeight: 15.w,
              imgWidth: 15.w,
              title: _areaCode,
              titleColor: ColorUtils.color_font_main,
              fontSize: 14.sp,
              margin: EdgeInsets.only(right: 10.w),
              onTapFunc: showTelCodeListPop,
            ),
            onValid: (bool vaild, String value) {
              _newPhone = value;
              setState(() {});
            })
        : InputView(
            title: LangOf(context)?.editPhone?.phone,
            hintText: LangOf(context)?.editPhone?.phonePhlane,
            inputViewType: InputViewType.InputViewTypePhone,
            value: _phone,
            onClickTextField: () {},
            onValid: (bool vaild, String value) => null);
  }

  captchaWidget() {
    return InputView(
        // key: ValueKey(SilTimeUtils.currentTimeMillis()),
        margin: EdgeInsets.only(top: 30.w),
        title: LangOf(context)?.editPhone?.code,
        hintText: LangOf(context)?.editPhone?.codePhlane,
        inputViewType: InputViewType.InputViewTypeCode,
        account: _isBinding ? _newPhone : _phone,
        showSendCode: true,
        showClearBtn: false,
        onValid: (bool vaild, String value) {
          _code = value;
        });
  }

  /// 新手机号
  newPhoneWidget() {
    return InputView(
        margin: EdgeInsets.only(top: 30.w),
        title: LangOf(context)?.editPhone?.new_phone,
        hintText: LangOf(context)?.editPhone?.newPhonePhlane,
        inputViewType: InputViewType.InputViewTypePhone,
        leftView: SilButton(
          padding: EdgeInsets.only(right: 5.w),
          imgPath: 'imgs/common/common_sanjx.png',
          imgHeight: 15.w,
          imgWidth: 15.w,
          title: _areaCode,
          titleColor: ColorUtils.color_font_main,
          fontSize: 14.sp,
          margin: EdgeInsets.only(right: 10.w),
          onTapFunc: showTelCodeListPop,
        ),
        onValid: (bool vaild, String value) {
          _newPhone = value;
          setState(() {});
        });
  }

  showTelCodeListPop() {
    SilBottomSheet.show(
        context: context,
        child: BottomSheetView(
          titles: _telCodeList,
          initialSelectedItem: _telCodeList.indexOf(_areaCode),
          tapSureSeletedFunc: (selected) {
            _areaCode = _telCodeList[selected];
            setState(() {});
          },
        ));
  }

  submitBtnWidget() {
    return Container(
      margin: EdgeInsets.only(top: 30.w),
      alignment: Alignment.center,
      child: SubmitBtn(
        margin: EdgeInsets.only(top: 20.w),
        title: LangOf(context)?.editNickName?.accomplish,
        onTapFunc: () {
          // 隐藏键盘
          globalTextFieldDismiss(context);
          _accountManagerController.requestEditPhone(
              context: context,
              phone: _newPhone,
              code: _code,
              areaCode: _areaCode,
              successHandler: (p0) => ARoute.pop(context));
        },
      ),
    );
  }
}
